<?php
class Project extends Zend_Db_Table_Abstract
{
	protected $_name = 'Project';
	
	// Check that the idProject is unique
	function checkUnique($idProject)
	{
		$select = $this->_db->select()
			->from($this->_name,array('idProject'))
			->where('idProject=?',$idProject);
		$result = $this->getAdapter()->fetchOne($select);
		if($result){
			return true;
		}
		return false;
	}
	
	// Get all the projects from the corresponding person
	function getProjects($idPerson)
	{
		$select = $this->select()->
			where('Person_idPerson = ?', $idPerson);
		$result = $this->fetchAll($select);
		
		return $result;
	}
	
	// Get the informations from a project
	public function getProject($idProject) 
	{
		$idProject = (int)$idProject;
		$row = $this->fetchRow('idProject = ' . $idProject);
		if (!$row) {
			throw new Exception("Count not find row $idProject");
		}
		return $row->toArray();	
	}
	 
	// Check that a project is owned by a Person
	public function checkProjectOwner($nameProject,$idPerson)
	{	
		$select = $this->_db->select()
			->from($this->_name,array('idProject'))
			->where('name=?',$nameProject)
			->where('Person_idPerson=?',$idPerson);
		$result = $this->getAdapter()->fetchOne($select);
		if($result){
			return true;
		}
		return false;
	}
	
	// Retrieve the Project id from the project name and the Person id
	public function getProjectId($nameProject,$idPerson)
	{
		$select = $this->_db->select('idProject')
			->from($this->_name,array('idProject'))
			->where('name=?',$nameProject)
			->where('Person_idPerson=?',$idPerson);
		$result = $this->getAdapter()->fetchOne($select);
		return $result;
	}
	
	// Retrieve the diagramm from the project name and the Person id
	public function getDiagramm($nameProject,$idPerson)
	{
		$select = $this->_db->select()
			->from($this->_name,array('idProject','diagramm'))
			->where('name=?',$nameProject)
			->where('Person_idPerson=?',$idPerson);
		$result = $this->getAdapter()->fetchAll($select);
		return $result;
	}
	
	// Update the diagramm from the corresponding project
	public function setDiagramm($idProject,$dia)
	{
		$data = array(
			'diagramm' => $dia
		);
		$this->update($data,'idProject='.(int)$idProject);
	}
	
	
	// Update the diagramm from the corresponding project
	public function setProjectInformations($idProject, $enterprise, $website, $startDate, $endDate, $note)
	{
		$data = array(
			'enterprise' => $enterprise,
			'website' => $website,
			'startDate' => $startDate,
			'endDate' => $endDate,
			'note' => $note,
		);
		$this->update($data,'idProject='.(int)$idProject);
	}
	
	// Delete the project which id is given
	public function deleteProject($idProject)
	{
		$this->delete('idProject=' . (int)$idProject);
	}
}

